﻿using MeterialManager.Aop.ServiceProcess;
using MeterialManager.Model;
using MeterialManager.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MeterialManager
{
    public partial class FrmSetWarehouse : Form
    {
        private DelBindDgv _delBindDgv;
        private WarehouseEntry _warehouseEntrie;
        public FrmSetWarehouse(DelBindDgv delBindDgv)
        {
            InitializeComponent();
            _delBindDgv = delBindDgv;
        }
        public FrmSetWarehouse(DelBindDgv delBindDgv,int id) : this(delBindDgv)
        {
            _warehouseEntrie = WarehouseEntry.ListAll().Find(m => m.Id == id);
        }

        private void FrmSetWarehouse_Load(object sender, EventArgs e)
        {
            if(_warehouseEntrie != null)
            {
                tbMeterialCode.Text = _warehouseEntrie.MeterialCode;
                tbMeterialName.Text = _warehouseEntrie.MeterialName;
                tbUnit.Text = _warehouseEntrie.Unit;
                tbRecipient.Text = _warehouseEntrie.Recipient;
                tbWarehouseNumber.Text = _warehouseEntrie.WarehouseNumber.ToString();
                tbRemark.Text = _warehouseEntrie.Remark;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string meterialCode = tbMeterialCode.Text.Trim();
            string meterialName = tbMeterialName.Text.Trim();
            string unit = tbUnit.Text.Trim();
            string recipient = tbRecipient.Text.Trim();
            decimal warehouseNumber = decimal.Parse(tbWarehouseNumber.Text.Trim());
            string remark = tbRemark.Text.Trim();
            if (_warehouseEntrie == null)
            {
                WarehouseEntry warehouseEntry = new WarehouseEntry()
                {
                    MeterialCode = meterialCode,
                    MeterialName = meterialName,
                    Unit = unit,
                    Recipient = recipient,
                    WarehouseNumber = warehouseNumber,
                    Remark = remark,
                    WarehouseDate = DateTime.Today
                };
                Func<dynamic, ResponseObject<int>> func = WarehouseEntry.Insert;
                IService iService = new Service();
                var service = ServiceProxy<IService>.Create(iService);
                ResponseObject<int> responseObject = service.DoSomething(func, warehouseEntry);
                if(responseObject.code == 200)
                {
                    MessageBox.Show("操作成功！");
                }
                else
                {
                    MessageBox.Show(responseObject.msg);
                }
            }
            else
            {
                WarehouseEntry warehouseEntry = new WarehouseEntry()
                {
                    WarehouseCode = _warehouseEntrie.WarehouseCode,
                    MeterialCode = meterialCode,
                    Recipient = recipient,
                    WarehouseNumber = warehouseNumber - _warehouseEntrie.WarehouseNumber,
                    Remark = remark,
                };
                Func<dynamic, ResponseObject<int>> func = WarehouseEntry.Update;
                IService iService = new Service();
                var service = ServiceProxy<IService>.Create(iService);
                ResponseObject<int> responseObject = service.DoSomething(func, warehouseEntry);
                if (responseObject.code == 200)
                {
                    MessageBox.Show("操作成功！");
                }
                else
                {
                    MessageBox.Show(responseObject.msg);
                }
            }
            _delBindDgv();
            this.Close();
        }

        private void tbMeterialCode_TextChanged(object sender, EventArgs e)
        {
            string meterialCode = tbMeterialCode.Text.Trim();
            Meterial selectMeterial = Meterial.ListAll().Find(m => m.MeterialCode == meterialCode);
            if (selectMeterial != null)
            {
                tbUnit.ReadOnly = true;
                tbMeterialName.ReadOnly = true;
                tbUnit.Text = selectMeterial.Unit;
                tbMeterialName.Text = selectMeterial.MeterialName;
            }
            else
            {
                tbUnit.ReadOnly = false;
                tbMeterialName.ReadOnly = false;
                tbUnit.Text = "";
                tbMeterialName.Text = "";
            }
        }
    }
}
